package com.woniuxy.oa.utils;

import java.util.Date;

import com.woniuxy.oa.entity.Employee;
import com.woniuxy.oa.exception.BirthdayException;
import com.woniuxy.oa.exception.ConfirmPasswordException;
import com.woniuxy.oa.exception.EmailException;
import com.woniuxy.oa.exception.EmployeeException;
import com.woniuxy.oa.exception.GenderException;
import com.woniuxy.oa.exception.IdcardException;
import com.woniuxy.oa.exception.NameException;
import com.woniuxy.oa.exception.PasswordException;
import com.woniuxy.oa.exception.TellphoneException;
import com.woniuxy.oa.exception.UsernameException;

public class EmployeeUtil {
	/**
	 * 该方法只验证格式，不验证与数据库相关
	 * */
	public static void verifyEmployee(Employee employee,String confirmPassword) throws EmployeeException{
		verifyUsername(employee.getUsername());
		verifyPassword(employee.getPassword());
		verifyConfirmPassword(employee.getPassword(),confirmPassword);
		verifyName(employee.getName());
		verifyTellphone(employee.getTellphone());
		verifyEmail(employee.getEmail());
		verifyIdcard(employee.getIdcard());
		verifyGender(employee.getGender());
		verifyBirthday(employee.getBirthday());
	}
	/**
	 * 用户名要求6-21位，字母开头
	 * */
	public static void verifyUsername(String username) throws UsernameException{
		if(username != null && !username.equals("")) {
			if(!RegexUtil.isUsername(username)) {
				throw new UsernameException("用户名格式不匹配");
			}
		}else {
			throw new UsernameException("用户名不能为空");
		}
	}
	/**
	 * 密码要求6-21位，字母或数字
	 * */
	public static void verifyPassword(String password) throws PasswordException{
		if(password != null && !password.equals("")) {
			if(!RegexUtil.isPassword(password)) {
				throw new PasswordException("密码格式不匹配");
			}
		}else {
			throw new PasswordException("密码不能为空");
		}
	}
	public static void verifyConfirmPassword(String password,String confirmPassword) throws PasswordException{
		if(confirmPassword == null || !confirmPassword.equals(password)) {
			throw new ConfirmPasswordException("两次密码名必须一致");
		}
	}
	/**
	 * 名字全中文
	 * */
	public static void verifyName(String name) throws NameException{
		if(name != null && !name.equals("")) {
			if(!RegexUtil.isChinese(name)) {
				throw new NameException("姓名必须为全中文");
			}
		}else {
			throw new NameException("姓名不能为空");
		}
	}
	/**
	 * 电话11位，详情见表达式
	 * */
	public static void verifyTellphone(String tellphone) throws TellphoneException{
		if(tellphone != null && !tellphone.equals("")) {
			if(!RegexUtil.isMobile(tellphone)) {
				throw new TellphoneException("号码格式不匹配");
			}
		}else {
			throw new TellphoneException("号码不能为空");
		}
	}
	/**
	 * 详情见正则util
	 * */
	public static void verifyEmail(String email) throws EmailException{
		if(email != null && !email.equals("")) {
			if(!RegexUtil.isEmail(email)) {
				throw new EmailException("email格式不匹配");
			}
		}else {
			throw new EmailException("email不能为空");
		}
	}
	/**
	 * 18位或15位
	 * */
	public static void verifyIdcard(String idcard) throws IdcardException{
		if(idcard != null && !idcard.equals("")) {
			if(!RegexUtil.isIDCard(idcard)) {
				throw new IdcardException("身份证格式不匹配");
			}
		}else {
			throw new IdcardException("身份证不能为空");
		}
	}
	/**男或女*/
	public static void verifyGender(String gender) throws GenderException{
		if(gender != null && !gender.equals("")) {
			if(!gender.equals("男") && !gender.equals("女")) {
				throw new GenderException("请输入正确的性别");
			}
		}else {
			throw new GenderException("性别不能为空");
		}
	}
	public static boolean isGender(String gender) {
		try {
			verifyGender(gender);
		} catch (GenderException e) {
			return false;
		}
		return true;
	}
	/**
	 * 必须在当前日期前
	 * */
	public static void verifyBirthday(String birthday) throws BirthdayException{
		Date birthDate = CurrentDateUtils.stringToDateWithoutTime(birthday);
		if(birthDate != null) {
			Date now = new Date();
			if(!now.after(birthDate)) {
				throw new BirthdayException("请输入正确的生日");
			}
		}else {
			throw new BirthdayException("生日不能为空");
		}
	}

}
